package com.wiz.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.wiz.vo.PayTaskVO;
import com.wiz.vo.to.PayConfigTO;
import com.wiz.vo.type.MerchantType;
import com.wiz.vo.type.PayChannelType;
import com.wiz.web.dao.mapper.PayMapper;
import com.wiz.web.service.LamicService;
import com.wiz.web.service.PayService;
import com.wiz.web.service.WjService;
import com.wiz.web.service.WxService;
import com.wiz.web.service.XlService;

@Service("payService")
public class PayServiceImpl extends BasicServiceImpl implements PayService{
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	//需要修改，引发了同步问题
	public static List<PayTaskVO> taskList = new ArrayList<PayTaskVO>();
	
	@Resource
	private WxService wxService;
	@Resource
	private WjService wjService;
	@Resource
	private XlService xlService;
	@Resource
	private LamicService lamicService;
	@Resource
	private PayMapper payMapper;
	
	@Override
	public String barPay(String order, String hqId,String stationId,int payChannel) throws Exception {
		log.info("stationId="+stationId+" barPay,params="+order+"  ,payChannel="+payChannel);
		PayConfigTO to = payMapper.getPayConfig(stationId);
		if(to == null){
			return RESPONSE_NO_PAY_CONFIG;
		}
		if(PayChannelType.WEIXIN.getId() == payChannel){
			if(StringUtils.isNotBlank(to.getStationWxMerchantId())){//优先采用站点账户号
				return wxService.wxBarPay(order, hqId, stationId, to.getStationWxMerchantId());
			}else if(StringUtils.isNotBlank(to.getHqWxMerchantId())){//其次采用总部账户号
				return wxService.wxBarPay(order, hqId, stationId, to.getHqWxMerchantId());
			}else{
				return RESPONSE_WX_NO_CONFIG;
			}
		}
		if(PayChannelType.WJI.getId() == payChannel){
			if(StringUtils.isNotBlank(to.getStationWjMerchantId())){//优先采用站点账户号
				return wjService.wjBarPay(order, hqId, stationId, to.getStationWjMerchantId());
			}else if(StringUtils.isNotBlank(to.getHqWjMerchantId())){//其次采用总部账户号
				return wjService.wjBarPay(order, hqId, stationId, to.getHqWjMerchantId());
			}else{
				return RESPONSE_WX_NO_CONFIG;
			}
		}
		if(PayChannelType.ZHIFUBAO.getId() == payChannel){//支付宝需要判定渠道
			if((to.getOnUsedMerchant() & MerchantType.LAMIC.getId()) == MerchantType.LAMIC.getId()){
				return lamicService.lamicBarPay(order, hqId, stationId, to.getLamicMerchantId(),
						to.getLamicMerchantToken());
			}
			if((to.getOnUsedMerchant() & MerchantType.XL.getId()) == MerchantType.XL.getId()){
				if(StringUtils.isNotBlank(to.getStationWxMerchantId())){//优先采用站点账户号
					return xlService.xlBarPay(order, hqId, stationId, to.getStationXlMerchantId(), payChannel,
							to.getXlTerminalId(), to.getXlSignKey());
				}else if(StringUtils.isNotBlank(to.getHqWxMerchantId())){//其次采用总部账户号
					return xlService.xlBarPay(order, hqId, stationId, to.getHqXlMerchantId(), payChannel,
							to.getXlTerminalId(), to.getXlSignKey());
				}else{
					return RESPONSE_WX_NO_CONFIG;
				}
			}
		}
		return RESPONSE_CODE_SERVER_ERROR;
	}
	
	@Override
	public String webPay(String order,String hqId,String stationId,int payChannel) throws Exception {
		log.info("stationId="+stationId+" webPay,params="+order+"  ,payChannel="+payChannel);
		PayConfigTO to = payMapper.getPayConfig(stationId);
		if(to == null){
			return RESPONSE_NO_PAY_CONFIG;
		}
		if(PayChannelType.WEIXIN.getId() == payChannel){
			if(StringUtils.isNotBlank(to.getStationWxMerchantId())){//优先采用站点账户号
				return wxService.wxWebPay(order, hqId, stationId, to.getStationWxMerchantId());
			}else if(StringUtils.isNotBlank(to.getHqWxMerchantId())){//其次采用总部账户号
				return wxService.wxWebPay(order, hqId, stationId, to.getHqWxMerchantId());
			}else{
				return RESPONSE_WX_NO_CONFIG;
			}
		}
//		if(PayChannelType.ZHIFUBAO.getId() == payChannel){
//			return aliBarPay(msg);
//		}
		return null;
	}

	@Override
	public String webCancel(String msg,String hqId,String stationId,int payChannel) throws Exception {
		PayConfigTO to = payMapper.getPayConfig(stationId);
		if(to == null){
			return RESPONSE_NO_PAY_CONFIG;
		}
		if(PayChannelType.WEIXIN.getId() == payChannel){
			if(StringUtils.isNotBlank(to.getStationWxMerchantId())){//优先采用站点账户号
				return wxService.closeWebPay(msg, hqId, stationId, to.getStationWxMerchantId());
			}else if(StringUtils.isNotBlank(to.getHqWxMerchantId())){//其次采用总部账户号
				return wxService.closeWebPay(msg, hqId, stationId, to.getHqWxMerchantId());
			}else{
				return RESPONSE_WX_NO_CONFIG;
			}
		}
//		if(PayChannelType.ZHIFUBAO.getId() == payChannel){
//			return aliBarPay(msg);
//		}
		return null;
	}
	
	
}
